<template>
  <div>
    <!--搜索框-->
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="课程">
        <el-select v-model="searchForm.courseId" placeholder="请选择课程" @change="selectCourseChange">
          <el-option :label="item.name" :value="item.id" v-for="item in courseData"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getPaperList">查看当前课程问卷</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">
          新增
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-popconfirm title="确定要批量删除吗？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <!--多选Tab-->
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column
          prop="title"
          label="问卷标题"
          width="120">
      </el-table-column>

      <el-table-column
          prop="remark"
          label="备注"
          width="120">
      </el-table-column>

      <el-table-column
          prop="questionCount"
          label="问题数量"
          width="120">
      </el-table-column>

      <el-table-column
          prop="startDate"
          label="预发布日期"
          width="120">
      </el-table-column>

      <el-table-column
          prop="status"
          label="发布状态"
          width="120">
      </el-table-column>

      <el-table-column
          prop="answerCount"
          label="已填写人数"
          width="120">
      </el-table-column>

      <el-table-column
          prop="noAnswerCount"
          label="未填写人数"
          width="120">
      </el-table-column>

      <el-table-column
          prop="studentCount"
          label="应填写人数"
          show-overflow-tooltip>
      </el-table-column>

      <el-table-column
          label="操作"
          width="180">
        <template slot-scope="scope">
          <el-button type="text" @click="goPaper(scope.row)">预览问卷</el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="editHandle(scope.row)" v-if="scope.row.status == 0">发布</el-button>
          <el-divider direction="vertical" v-if="scope.row.status == 0"></el-divider>

          <el-popconfirm title="确定要删除这条记录吗？" @confirm="delHandle(scope.row.id)">
            <!--<el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>-->
            <el-button type="text" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>

    <!--新增对话框-->
    <el-dialog title="新增课程问卷" :visible.sync="dialogFormVisible" width="800px" @closed="resetForm('editForm')">

      <el-form :model="editForm" ref="editForm" :rules="editFormRules" label-width="100px" class="demo-dynamic">
        <!--<el-form-item label="课程">-->
        <!--  <el-select v-model="editForm.courseId" placeholder="请选择课程" @change="selectCourseChange">-->
        <!--    <el-option :label="item.name" :value="item.id" v-for="item in courseData"></el-option>-->
        <!--  </el-select>-->
        <!--</el-form-item>-->

        <el-form-item
            prop="title"
            label="问卷标题"
        >
          <el-input v-model="editForm.title" prefix-icon="el-icon-document-copy"></el-input>
        </el-form-item>
        <el-form-item
            prop="remark"
            label="问卷备注"
        >
          <el-input v-model="editForm.remark"></el-input>
        </el-form-item>

        <el-form-item label="开始时间">
          <el-date-picker
              v-model="editForm.startDate"
              type="datetime"
              placeholder="选择日期时间"
              default-time="00:00:00">
          </el-date-picker>
        </el-form-item>

        <el-form-item
            v-for="(question, index) in editForm.questionList"
            :label="'问题' + index"
            :key="question.key"
            :prop="'questionList.' + index + '.value'"
        >
          <el-input v-model="question.content" prefix-icon="el-icon-search"></el-input>
          <el-select placeholder="请选择一个课程目标" v-model="question.targetId">
            <el-option :label="item.content" :value="item.id" v-for="item in targetList"></el-option>
          </el-select>

          <!--<el-input placeholder="请输入问题" v-model="question.content" class="input-with-select" >
            <el-select placeholder="请选择一个课程目标" v-model="question.target_id" slot="prepend">
              <el-option label="餐厅名" value="1" ></el-option>
            </el-select>
          </el-input>-->

          <el-button @click.prevent="removeDomain(question)">删除</el-button>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditForm('editForm')">提交</el-button>
        <el-button @click="addDomain">添加问题</el-button>
        <el-button @click="resetForm('editForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Paper",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,
      dialogFormVisible: false,
      total: 0,
      size: 10,
      current: 1,
      editForm: {
        courseId: '',
        title: '',
        remark: '',
        startDate: '',
        status: '',
        questionList: [{
          paperId: '',
          targetId: '',
          content: '',
        }]
      },
      courseData: [],
      pointList: [],
      targetList: [],
      studentList: [],
      tableData: [],
      editFormRules: {
        title: [
          {required: true, message: '请输入问卷标题', trigger: 'blur'}
        ]
      },
      multipleSelection: []
    }
  },
  created() {
    this.getCourseList()
    /*this.$axios.get("/course").then(res => {
      this.courseData = res.data.data.records
      this.pointList = res.data.data.records.pointList
      this.targetList = res.data.data.records.targetList
      console.log(this.targetList)
    })*/
    this.$axios.get("/student").then(res => {
      this.studentList = res.data.data.records
    })
  },
  methods: {
    selectCourseChange(event) {
      console.log(event)
      for (const element of this.courseData) {
        console.log(element)
        if (element.id === event) {
          this.targetList = element.targetList
          this.editForm.courseId = element.id
          console.log(this.editForm.courseId)
        }
      }
      console.log(this.targetList)
    },
    getCourseList() {
      this.$axios.get("/course", {
        params: {
          current: 1,
          size: 10000
        }
      }).then(res => {
        this.courseData = res.data.data.records
        this.pointList = res.data.data.records.pointList
        this.targetList = res.data.data.records.targetList
        console.log(this.targetList)
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.delBtlStatu = val.length === 0
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getPaperList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getPaperList()
    },
    resetForm(formName) {
      this.editForm = {
        id: null,
        courseId: '',
        title: '',
        remark: '',
        startDate: '',
        status: '',
        questionList: [{
          paperId: '',
          targetId: '',
          content: '',
        }]
      }
      this.dialogFormVisible = false
      // this.getPaperList()
    },
    handleClose() {
      this.resetForm('editForm')
    },
    getPaperList() {
      this.$axios.get("/paper", {
        params: {
          courseId: this.searchForm.courseId,
          current: this.current,
          size: this.size
        }
      }).then(res => {
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },
    submitEditForm(formName) {
      console.log(this.editForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/paper', this.editForm)
              .then(res => {
                this.getPaperList()
                console.log(res.data)
                this.resetForm(formName)
                this.$message({

                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                });
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(data) {
      console.log(data)
      this.editForm = data
      this.editForm.status = 1
      this.$axios.post('/paper', this.editForm)
          .then(res => {
            this.getPaperList()
            this.$message({
              showClose: true,
              message: '发布问卷成功',
              type: 'success',
            });
          })
      this.getPaperList()
      // this.dialogFormVisible = true
    },
    goPaper(data) {
      /*console.log(data)
      console.log(this.$route)
      console.log(this.$router)
      console.log(window.location.origin)*/
      this.$router.push("/answer/" + data.id)
    },
    delHandle(id) {
      var ids = []
      if (id) {
        ids.push(id)
      } else {
        // 批量删除
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }
      console.log(ids)
      this.$axios.delete("/paper", {
        data: ids
      }).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
        });
        // 刷新列表
        this.getPaperList()
      })
    },
    removeDomain(item) {
      var index = this.editForm.questionList.indexOf(item)
      if (index !== -1) {
        this.editForm.questionList.splice(index, 1)
      }
    },
    addDomain() {
      console.log(this.targetList)
      console.log(this.editForm.questionList)
      this.editForm.questionList.push({
        /*value: '',
        key: Date.now()*/
        paperId: '',
        targetId: '',
        content: '',
      });

    }
  }
}
</script>

<style scoped>

/*
.el-input {
  width: 350px;
}
*/

.el-select .el-input {
  width: 130px;
}

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.el-pagination {
  float: right;
  margin-top: 10px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>